# Code of vsmed #!/usr/bin/env python """ vsmed Compute local 1x1x1 median using the buffer method """ from v4 import vx from vxbuffer import * import sys vargs = vx.vaparse( "if= of= n= -v - ") if '-' in vargs: print ("vssump 1x1xn local temporal median filter") print ("if= input file") print ("of= output file") print ("n= number of frames") print ("[-v] verbose mode for very small images") sys.exit(0) for arg in ['if', 'of', 'n']: if arg not in vargs: print ('vssump error: required parameter %s is missing' % arg) sys.exit(1) optv = '-v' in vargs nn = int(vargs['n']) invx = vxIbuf(vargs['if'], nn); outvx = vxObuf(vargs['of'] ); im = invx.i imr = empty( im[0].shape, dtype=im.dtype); while invx.read(): im = invx.i num1 = 0 num2 = 0 num3 = 0 for y in range(im.shape[1] ): for x in range(im.shape[2]): num1 = im[0][y][x] num2 = im[1][y][x] num3 = im[2][y][x] if num1 >= num2 and num1 <= num3: imr[y][x] = num1 elif num1 <= num2 and num1 >= num3: imr[y][x] = num1 elif num2 <= num1 and num2 >= num3: imr[y][x] = num2 elif num2 >= num1 and num2 <= num3: imr[y][x] = num2 else: imr[y][x] = num3 if optv: print (imr) outvx.add(imr) outvx.close()
Idea of the Challenge problem: In the output images, if in a certain square region (e.g. 10*10), more than about 90% of the pixel is in white color, then this region should be a large moving part. The reason is that if the object is not moving, the proportion of white pixels will be less due to the random noise in black pixels in the background. We can do padding in the edges, perform checking on each location, and mark the locations that satisfy the condition with red color.# Code of vsdif #!/usr/bin/env python """ vsdif Detect difference in 1x1x1 region using the buffer method """ from v4 import vx from vxbuffer import * import sys vargs = vx.vaparse( "if= of= th= -v - ") if '-' in vargs: print ("vsdif 1x1xn difference detector") print ("if= input file") print ("of= output file") print ("th= threshold value") print ("[-v] verbose mode for very small images") sys.exit(0) for arg in ['if', 'of', 'th']: if arg not in vargs: print ('vsdif error: required parameter %s is missing' % arg) sys.exit(1) optv = '-v' in vargs th = int(vargs['th']) invx = vxIbuf(vargs['if'], 2); outvx = vxObuf(vargs['of'] ); im = invx.i imr = empty( im[0].shape, dtype=im.dtype); while invx.read(): im = invx.i for y in range(im.shape[1] ): for x in range(im.shape[2]): if (abs(im[0][y][x] - im[1][y][x]) > th): imr[y][x] = 255 else: imr[y][x] = 0 if optv: print (imr) outvx.add(imr) outvx.close()